//
//  MyQueryDetailViewController.m
//  NewApp
//
//  Created by xueyan liu on 7/15/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MyQueryDetailViewController.h"

#import "ProductData.h"
#import "CustomTableCellB.h"

@interface MyQueryDetailViewController ()

@end

@implementation MyQueryDetailViewController

@synthesize userID;
@synthesize ID;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.listStyle = @"B";
    self.dataArray = [NSMutableArray arrayWithCapacity:10];
    
    [self.engine myOrderViewWithID:userID Q1:ID];
    
    _bottomCell = [[UITableViewCell alloc] init];
    _bottomCell.contentView.backgroundColor = [UIColor redColor];
    _bottomCell.textLabel.backgroundColor = [UIColor clearColor];
    _bottomCell.textLabel.textColor = [UIColor whiteColor];
    _bottomCell.textLabel.textAlignment = UITextAlignmentCenter;
}

- (void)dealloc {
    
    [userID release];
    [ID release];
    
    [super dealloc];
}

#pragma mark -
#pragma mark UITableViewDataSource & UITableViewDelegate Support.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([[dataArray objectAtIndex:indexPath.row] isKindOfClass:[NSString class]]) {
        
        return 44.0f;
    }else {
        
        return 90.0f;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if([[dataArray objectAtIndex:indexPath.row] isKindOfClass:[NSString class]]) {
        
        _bottomCell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
        return _bottomCell;
    }
    
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    // Configure the cell...
    MyOrderViewEntity *entity = [dataArray objectAtIndex:indexPath.row];
    
    [cell.backgroundView setFrame:CGRectMake(0, 0, 320, 90)];
    cell.backgroundColor=[UIColor clearColor];
    if (indexPath.row % 2 == 0 ) {
        cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"news_list_B_item_bg_1.png"]]; 
    }else {
        cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"news_list_B_item_bg_2.png"]];
    }
    
    UITableViewCell *celltmp;
    if([listStyle isEqualToString:@"B"]) {
        
        celltmp = [[CustomTableCellB alloc] init];
        [(CustomTableCellB *)celltmp setContentWithImage:[NSURL URLWithString:entity.img]
                                                   title:[NSString stringWithFormat:@"商品型號:%@", entity.Item_no]
                                                subtitle:[NSString stringWithFormat:@"%@\n數量:%@", entity.Item_name, entity.num] 
                                                 content:[NSString stringWithFormat:@"小計:%@$", entity.sprice]];
    }
    
    [cell addSubview:celltmp];
    
    return cell;
}

#pragma mark -
#pragma mark Request Engine Support.

- (void)requestDidFinished:(RequestEngine *)reqEngine withObject:(id)object {
    
    [dataArray removeAllObjects];
    
    NSArray *results = [(NSDictionary *)object objectForKey:@"data"];
    for (NSDictionary *subDict in results) {
        
        [dataArray addObject:[[MyOrderViewEntity alloc] initWithDictionary:subDict]];
    }
    
    [dataArray makeObjectsPerformSelector:@selector(release)];
    
    [dataArray addObject:[NSString stringWithFormat:@"總計%@", [(NSDictionary *)object objectForKey:@"total"]]];
    
    [productTable reloadData];
}

@end
